const ctxinitialState = () => ({
	searchParam: {
		keyword: undefined,
		startCreationTime: undefined,
		endCreationTime: undefined,
	},
});
// 使用 reactive 初始化
const ctx = reactive(ctxinitialState());

export const initialize = (editRef: globalThis.Ref<any, any>, filteredList: globalThis.Ref<any, any>, list: globalThis.Ref<any, any>) => {
	// 新增操作
	const handleAdd = () => {
		editRef.value.defaultValue();
		editRef.value.openDrawerFn();
	};
	//检索查询列表操作
	const search = () => {
		const keyword = ctx.searchParam.keyword;
		// 如果没有关键字，返回全部数据
		if (!keyword) {
			filteredList.value = list.value;
			return;
		}

		filteredList.value = list.value.filter((item: { name: string; projectCode: string; textCode: string }) => {
			return (
				(item.name != undefined && item.name.toLowerCase().includes(keyword)) ||
				(item.projectCode != undefined && item.projectCode.toLowerCase().includes(keyword)) ||
				(item.textCode != undefined && item.textCode.toLowerCase().includes(keyword))
			);
		});
	};
	//提交成功后的回调
	const updata = (data: any) => {
		let index = list.value.findIndex((t: { id: any }) => t.id == data.value.id);
		if (index != -1) {
			filteredList.value[index] = data.value;
		} else {
			data.value.id = list.value.length + 1;
			list.value.push(data.value);
			filteredList.value = [...list.value];
		}
	};
	//双击列表操作
	const rowDbClick = (row: any) => {
		editRef.value.openDrawerFn();
		editRef.value.dataAssignment(row);
	};

	return { ctx, handleAdd, search, updata, rowDbClick };
};
